//
//  HotMovieViewController.m
//  A段项目
//
//  Created by ～whc on 15-6-17.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "HotMovieViewController.h"

@interface HotMovieViewController ()

@end

@implementation HotMovieViewController
- (void)dealloc
{
    [self.array release];
    [self.hotMovieTable release];
    [self.hotMovieScroller release];
    [self.arrayCache release];
    [self.arrayId release];
    [self.HUD release];
    [self.movieDetail dealloc];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view setBackgroundColor:[UIColor whiteColor]];
    self.navigationController.navigationBar.translucent = NO;
    
    [self createTable];
    [[DateHandel shareHandle]createTable];
    
    [self afnGetRequest];
    
//    [self createScroll];
}



//下拉刷新
-(void)headerRefesh
{
    self.hotMovieTable.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.isUpLoading = NO;
        
        self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        self.HUD.labelText = @"正在加载中";
        [self.HUD show:YES];
        [self afnGetRequest];
        
        [self.hotMovieTable reloadData];
        [self.HUD hide:YES];
        // 拿到当前的下拉刷新控件，结束刷新状态
        [self.hotMovieTable.header endRefreshing];
        
    }];
    
    
}


- (void)createTable
{
#warning 创建tableView
    self.hotMovieTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-100) style:UITableViewStylePlain];
//    [self.hotMovieTable setBackgroundColor:[UIColor blueColor]];
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载中";
    [self.HUD show:YES];

    [self.view addSubview:self.hotMovieTable];
    
    self.hotMovieTable.delegate = self;
    self.hotMovieTable.dataSource = self;
    
    
    [self headerRefesh];
}

- (void)createScroll
{
#warning 创建ScrollerView
    self.hotMovieScroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 220)];
    [self.hotMovieScroller setBackgroundColor:[UIColor redColor]];
    self.hotMovieTable.tableFooterView = self.hotMovieScroller;
    //    [self.hotMovieScroller];
    
}

#warning tableView必须实现的方法

//共有多少个cell
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.array.count;
    
}
//每个cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (WIDTH == 320 && HEIGHT == 568) {
        return 170;
    }
    return 190;
}
//自定义cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"hotMovieCell";
    HotMovieTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[HotMovieTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    Movie *mov = [self.array objectAtIndex:indexPath.row];
    cell.mov = mov;
    return cell;
}


#warning 实现点击进入事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.hotMovieTable deselectRowAtIndexPath:indexPath animated:YES];
    
    [UIView beginAnimations:@"animationID" context:nil];    [UIView setAnimationDuration:0.5f];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];    [UIView setAnimationRepeatAutoreverses:NO];

        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:[tableView cellForRowAtIndexPath:indexPath] cache:YES];

    [UIView commitAnimations];
    
    
    self.movieDetail = [[MovieDetailViewController alloc] init];
    self.movieDetail.hidesBottomBarWhenPushed = YES;
    Movie *mov = [self.array objectAtIndex:indexPath.row];
    self.movieDetail.movieID = mov.movieId;
    [self.navigationController pushViewController:self.movieDetail animated:YES];
    
    [self.movieDetail release];
    NSLog(@"%ld",self.movId);

    
}
-(void)afnGetRequest
{
   self.array = [NSMutableArray array];
    self.array = [[DateHandel shareHandle]selectAllMovie];
    
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    NSString *url_string = @"http://api.m.mtime.cn/Showtime/LocationMovies.api?locationId=729";
    
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"application/x-javascript",@"text/html",nil];

    
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
//            NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
        [[DateHandel shareHandle]dropTable];
        [[DateHandel shareHandle]createTable];
        [self.array removeAllObjects];
        
        NSMutableArray *array = [NSMutableArray array];
        array = [responseObject objectForKey:@"ms"];
        
        self.arrayId = [NSMutableArray array];
    
//        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        for (NSMutableDictionary *dic in array) {
            NSMutableArray *arr = [NSMutableArray array];
            arr = [dic objectForKey:@"versions"];
            Movie *mov = [[Movie alloc]init];
            mov.t = [dic objectForKey:@"t"];
            mov.movieId = [[dic objectForKey:@"id"]integerValue];
            mov.r = [[dic objectForKey:@"r"]floatValue];
            mov.img = [dic objectForKey:@"img"];
            mov.rd = [dic objectForKey:@"rd"];
            NSString *show = [dic objectForKey:@"isNew"];
            mov.isNew = [NSString stringWithFormat:@"%@",show];
            mov.commonSpecial = [dic objectForKey:@"commonSpecial"];
            mov.NearestCinemaCount = [dic objectForKey:@"NearestCinemaCount"];
            mov.NearestShowtimeCount = [dic objectForKey:@"NearestShowtimeCount"];
            NSString *bo1 = [dic objectForKey:@"is3D"];
            mov.is3D = [NSString stringWithFormat:@"%@",bo1];
            NSString *bo2 = [dic objectForKey:@"isIMAX3D"];
            mov.isDMAX = [NSString stringWithFormat:@"%@",bo2];
            NSString *bo3 = [dic objectForKey:@"isDMAX"];
            mov.isIMAX3D = [NSString stringWithFormat:@"%@",bo3];
            [self.array addObject:mov];
            [self.arrayId addObject:[NSString stringWithFormat:@"%ld",mov.movieId]];
            [[DateHandel shareHandle]insertMovie:mov];
            [mov release];
           
        }
        
        [self.hotMovieTable reloadData];
         [self.HUD hide:YES];
  
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        [self.HUD hide:YES];
        
    }];
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
